import { getAuthorize } from '@/api/login'

const user = {
  state: {
    id: '',
    nickname: '',
    code: '',
    auth_token: '',
    name: ''
  },
  getters: {
    isLogin: state => {
      if (process.env.NODE_ENV === 'development') {
        return true
      }
      return (typeof state.auth_token === 'string') && Boolean(state.auth_token)
    }
  },
  mutations: {
    updateUserInfo (state, payload) {
      Object.assign(state, payload)
      localStorage.setItem('user', JSON.stringify(state))
    },
    SET_NAME: (state, name) => {
      state.name = name
    }
  },

  actions: {
    userSignin () {
      let redirectUrl = window.location.protocol + '//' + window.location.host
      getAuthorize({redirect_url: redirectUrl}).then(
        response => {
          console.log('data: ', response.data)
          window.location.replace(response.data.url)
        }
      )
      // Vue.axios.get('api/v1/authorize', {params: {redirect_url: redirectUrl}}).then(
      //   response => {
      //     window.location.replace(response.data.data.url)
      //   }
      // )
    }
    // 用户名登录
    // LoginBy({ commit }) {
    //   return new Promise((resolve, reject) => {
    //     loginByUsername().then(response => {
    //       const data = response.data
    //       commit('SET_TOKEN', data.token)
    //       setToken(response.data.token)
    //       resolve()
    //     }).catch(error => {
    //       reject(error)
    //     })
    //   })
    // }
  }
}
export default user
